$(function() {
	// 供应商的数据绑定
	var userVue = new Vue({
		el : '#userDiv',
		data : {
			items : null
		},
		methods : {
			// 编辑按钮的点击
			editItem : function(user) {
				openEdituserBox(user);
			},
			// 删除按钮的点击
			deleteItem : function(user) {
				openDeleteConfirm(user);
			}
		}
	});
	// ajax 获得所有的用户
	getAllUser();

	$("#createUserButton").on("click", function() {
		openCreateuserBox();
	});

	function openCreateuserBox() {
		var createuserBox = layer.open({
			type : 2,
			maxmin : true,
			title : '新建用户',
			offset : '4%',
			area : [ '70%', '92%' ],
			btn : [ '确定', '取消' ] // 只是为了演示
			,
			yes : function() {
				// 获得子窗口的值
				var childBox = layer.getChildFrame('body', createuserBox);

				/*
				 * var data = { "userName" :
				 * childBox.find("#inputUserName").val(), "nickName" :
				 * childBox.find("#inputNickName").val(), "password" :
				 * childBox.find("#inputPassword").val(), "phoneNumber" :
				 * childBox.find("#inputPhoneNumber").val(), "roleId" :
				 * childBox.find("#inputRole").val(), "remark" :
				 * childBox.find("#inputRemark").val() };
				 */

				// 获得form表单的数据
				var data = childBox.find("#userForm").serialize();

				baseUtil.httpSend("user/createNewUserAjax", data,
						showUpdatedDataByResult);
				layer.close(createuserBox);

			},
			btn2 : function() {
				layer.close();
			},
			shadeClose : true, // 点击遮罩关闭
			content : JSP_PRE_URL + 'editUser'
		});
	}

	function openEdituserBox(user) {

		var edituserBox = layer.open({
			type : 2,
			maxmin : true,
			title : '编辑用户',
			offset : '4%',
			area : [ '70%', '92%' ],
			btn : [ '确定', '取消' ] // 只是为了演示
			,
			yes : function() {
				// 获得子窗口的值
				var childBox = layer.getChildFrame('body', edituserBox);
				// 获得form表单的数据
				var data = childBox.find("#userForm").serialize();

				baseUtil.httpSend("user/updateUserAjax", data,
						showUpdatedDataByResult);
				layer.close(edituserBox);

			},
			btn2 : function() {
				layer.close();
			},
			shadeClose : true, // 点击遮罩关闭
			content : SERVER_PRE_URL
					+ 'user/getUserByPrimaryKeyReturnTo?userId=' + user.userId
					+ '&returnTo=editUser'
		});

	}

	function openDeleteConfirm(user) {
		// 询问框
		layer.confirm('确认删除：' + user.nickname, {
			offset : '20%',
			btn : [ '确认', '取消' ]
		// 按钮
		}, function() {
			deleteuserDetail(user.userId);
		}, function() {
		});
	}

	function deleteuserDetail(id) {
		var data = {
			"userId" : id
		};
		baseUtil.httpSend("user/deleteUserAjax", data, showUpdatedDataByResult);
	}

	function showUpdatedDataByResult(result) {
		if (result == 1) {
			getAllUser();
			layer.msg('操作成功');
		} else {
			layer.msg('操作失败');
		}
	}

	function getAllUser() {
		baseUtil.httpSend("user/getAllUserAjax", null, showuserData);
	}

	function showuserData(data) {
		userVue.items = data;
	}

});